package com.socket;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.CharsetUtil;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class Client {

    public static void main(String[] args) {
        try {
            Socket socket = new Socket("127.0.0.1", 55511);
            Console.log("socket is connected success: {}", socket.getLocalPort());
            InputStream in = socket.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
            OutputStream out = socket.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(out));
            for (int i = 0; i < 1; i++) {
                String msg = "push a msg " + i + "\n";
                bufferedWriter.write(msg);
                bufferedWriter.flush();
                String answer = bufferedReader.readLine();
                Console.log("server answer msg:{}", answer);
            }
            System.out.println("client closing");
        socket.shutdownOutput();
        socket.shutdownInput();
        socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
